use std::process::Command;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::test]
async fn test_project_structure() {
    // 测试项目结构是否正确
    assert!(std::path::Path::new("backend/shared/Cargo.toml").exists());
    assert!(std::path::Path::new("backend/api-gateway/Cargo.toml").exists());
    assert!(std::path::Path::new("frontend/package.json").exists());
    assert!(std::path::Path::new("docker-compose.yml").exists());
    assert!(std::path::Path::new("README.md").exists());
    assert!(std::path::Path::new("plan1.md").exists());
}

#[tokio::test]
async fn test_backend_compilation() {
    // 测试后端代码是否能编译
    let output = Command::new("cargo")
        .args(&["check", "--manifest-path", "backend/shared/Cargo.toml"])
        .output()
        .expect("Failed to execute cargo check");

    if !output.status.success() {
        println!("Cargo check output: {}", String::from_utf8_lossy(&output.stderr));
    }

    assert!(output.status.success(), "Backend shared compilation failed");
}

#[tokio::test]
async fn test_frontend_dependencies() {
    // 测试前端依赖是否正确
    let package_json = std::fs::read_to_string("frontend/package.json")
        .expect("Failed to read package.json");
    
    assert!(package_json.contains("react"));
    assert!(package_json.contains("typescript"));
    assert!(package_json.contains("vite"));
    assert!(package_json.contains("antd"));
}

#[tokio::test]
async fn test_docker_compose_syntax() {
    // 测试Docker Compose文件语法
    let output = Command::new("docker-compose")
        .args(&["config"])
        .output();

    if let Ok(output) = output {
        assert!(output.status.success(), "Docker Compose syntax error");
    } else {
        println!("Docker Compose not available, skipping test");
    }
}

#[test]
fn test_api_routes_defined() {
    // 测试API路由是否正确定义
    let routes_content = std::fs::read_to_string("backend/api-gateway/src/routes.rs")
        .expect("Failed to read routes.rs");
    
    assert!(routes_content.contains("/health"));
    assert!(routes_content.contains("/connectors"));
    assert!(routes_content.contains("/connections"));
    assert!(routes_content.contains("/pipelines"));
    assert!(routes_content.contains("/topics"));
    assert!(routes_content.contains("/ai"));
}

#[test]
fn test_database_migrations() {
    // 测试数据库迁移文件是否存在
    assert!(std::path::Path::new("backend/migrations/001_initial.sql").exists());
    
    let migration_content = std::fs::read_to_string("backend/migrations/001_initial.sql")
        .expect("Failed to read migration file");
    
    assert!(migration_content.contains("CREATE TABLE connectors"));
    assert!(migration_content.contains("CREATE TABLE connections"));
    assert!(migration_content.contains("CREATE TABLE pipelines"));
}
